package thirdFragment.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.bigkoo.convenientbanner.listener.OnItemClickListener;
import com.example.xuegengwang.xuegengwang.R;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import common.BaseFragment;
import common.Constant;
import https.InterNetController;
import https.handler.ErrorPageHandler;
import in.srain.cube.views.ptr.PtrDefaultHandler2;
import in.srain.cube.views.ptr.PtrFrameLayout;
import view.MyPtrRefresh_LoadView;
import view.NetworkImageHolderView;
import thirdFragment.adapter.ExamLvAdapter;
import thirdFragment.bean.ExamBean;

@SuppressLint("ValidFragment")
public class HighSchoolEntranceExam extends BaseFragment implements OnItemClickListener {
    private View header;
    private View view;
    private MyPtrRefresh_LoadView exam_refresh_layout;
    private ListView exam_lv;
    private ExamLvAdapter examLvAdapter;
    private int page = 1;//已经加载的当前页
    private List<String> networkImages;
    private ConvenientBanner convenientBanner;//轮播图
    private final int INIT = 0;
    private final int REFRESH = 1;
    private final int ONLOAD = 2;
    private ArrayList<ExamBean.NewsListBean> arrayList;
    private ArrayList<ExamBean.BannerBean> banner;//轮播图数组
    private int topNewsNum;//置顶位置
    private ErrorPageHandler myHandler = new ErrorPageHandler(true) {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case Constant.ERROR_CODE_SUCCESS:
                    Gson gson = new Gson();
                    ExamBean examBean = gson.fromJson((String) msg.obj, ExamBean.class);
                    if (examBean != null) {
                        List<ExamBean.BannerBean> banner = examBean.getBanner();
                        if (banner != null) {
                            HighSchoolEntranceExam.this.banner = (ArrayList<ExamBean.BannerBean>) banner;
                            setBanner(HighSchoolEntranceExam.this.banner);
                        }
                    }
                    ArrayList<ExamBean.NewsListBean> newsList = (ArrayList<ExamBean.NewsListBean>) examBean.getNewsList();
                    for (int i = 0; i < newsList.size(); i++) {
                        if ("1".equals(newsList.get(i).getIs_top())) {
                            // 文章置顶
                            topNewsNum = i;
                        } else {
                            break;
                        }
                    }
                    switch (msg.arg1) {
                        case INIT:
                            if (newsList != null) {
                                arrayList.clear();
                                for (int i = 0; i < newsList.size(); i++) {
                                    arrayList.add(newsList.get(i));
                                }
                            }
                            break;
                        case REFRESH:
                            if (newsList != null) {
                                for (int i = 0; i < newsList.size(); i++) {
                                    arrayList.add(topNewsNum, newsList.get(i));
                                }
                            }
                            break;
                        case ONLOAD:
                            if (newsList != null) {
                                for (int i = 0; i < newsList.size(); i++) {
                                    arrayList.add(newsList.get(i));
                                }
                            }
                            break;
                        default:
                            break;
                    }
                    examLvAdapter.notifyDataSetChanged();
                    break;
                case Constant.ERROR_CODE_FINISH:
                    exam_refresh_layout.OnRefreshComplete();
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getMyActivity() != null) {
            if (view == null) {
                view = LayoutInflater.from(getMyActivity()).inflate(R.layout.fragment_high_school_entrance_exam, null);
            }
            initView();
            initListView();
            getDataFromInternet(page, INIT, true);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return view;
    }

    private void initView() {
        header = LayoutInflater.from(getMyActivity()).inflate(R.layout.exam_header, null);
        convenientBanner = (ConvenientBanner) header.findViewById(R.id.exam_banner);
        exam_refresh_layout = (MyPtrRefresh_LoadView) view.findViewById(R.id.exam_refresh_layout);
        exam_refresh_layout.setPtrHandler(new PtrDefaultHandler2() {
            @Override
            public void onLoadMoreBegin(PtrFrameLayout frame) {
                page += 1;
                getDataFromInternet(page, ONLOAD, false);
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                if (arrayList.size() == 0) {
                    getDataFromInternet(page, INIT, true);
                } else {
                    getDataFromInternet(++page, REFRESH, false);
                }
            }
        });
    }

    private void initListView() {
        arrayList = new ArrayList<>();
        examLvAdapter = new ExamLvAdapter(getMyActivity(), arrayList);
        exam_lv = (ListView) view.findViewById(R.id.exam_lv);
        exam_lv.addHeaderView(header);
        exam_lv.setAdapter(examLvAdapter);
    }

    private void setBanner(ArrayList<ExamBean.BannerBean> bannerBeenList) {
        networkImages = new ArrayList<>();
        for (int i = 0; i < bannerBeenList.size(); i++) {
            networkImages.add(bannerBeenList.get(i).getImage());
        }
        //设置轮播图片
        convenientBanner.setPages(new CBViewHolderCreator<NetworkImageHolderView>() {
            @Override
            public NetworkImageHolderView createHolder() {
                return new NetworkImageHolderView();
            }
        }, networkImages)
                .setPointViewVisible(true)    //设置指示器是否可见
                .startTurning(2000)
                .setPageIndicator(new int[]{R.mipmap.main_doc1, R.mipmap.main_doc})
                .setPageIndicatorAlign(ConvenientBanner.PageIndicatorAlign.ALIGN_PARENT_RIGHT)
                .setOnItemClickListener(this);   //设置指示器圆点;
    }


    private void getDataFromInternet(int page, int flag, boolean waitingDialog) {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("page", String.valueOf(page));
        hashMap.put("type", "exam");
        new InterNetController(getMyActivity(), Constant.NEWLIST, myHandler, hashMap, flag, waitingDialog);
    }

    @Override
    public void onItemClick(int position) {
        //点击banner图片事件
        if (banner.get(position).getLink() != null) {
            getMyActivity().drumpActivity(banner.get(position).getLink());
        }
    }
}
